package org.wmsafe.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.wmsafe.pojo.*;
import org.wmsafe.service.*;
import org.wmsafe.service.impl.HeadLinkServiceImpl;

import java.util.*;

@Controller
public class IndexController {


    LinksTypeService linksTypeService;

    HeadLinkService headLinkService;

    FooterLinkService footerLinkService;

    LinksService linksService;

    @Autowired
    public void setLinksTypeService(LinksTypeService linksTypeService) {
        this.linksTypeService = linksTypeService;
    }

    @Autowired
    public void setHeadLinkService(HeadLinkService headLinkService) {
        this.headLinkService = headLinkService;
    }

    @Autowired
    public void setFooterLinkService(FooterLinkService footerLinkService) {
        this.footerLinkService = footerLinkService;
    }

    @Autowired
    public void setLinksService(LinksService linksService) {
        this.linksService = linksService;
    }

    @RequestMapping("/")
    public ModelAndView index() {
        /**
         * ModelAndView有Model和View的功能
         * Model主要用于向请求域共享数据
         * View主要用于设置视图，实现页面跳转
         */
        ModelAndView mav = new ModelAndView();
        //向请求域共享数据
        mav.addObject("testScope", "hello,ModelAndView");
        List<LinksType> linksTypesList = linksTypeService.list();
        List<HeadLink> headlinkList = headLinkService.list();

        //代理角色，没有，这里通过ProxyHeadLinkService动态创建一个
        ProxyHeadLinkService proxyHeadLinkService = new ProxyHeadLinkService();
        //设置需要代理的真实角色
        proxyHeadLinkService.setHeadLinkService(headLinkService);
        //获取到动态生成的代理角色
        HeadLinkService proxy = (HeadLinkService) proxyHeadLinkService.getProxy();
        //通过代理角色调用原有的list方法，并且在这之前，自己新增的功能也会执行
        headlinkList = proxy.list();

        List<FooterLink> footerlinkList = footerLinkService.list();
        // 放入转发参数
        mav.addObject("linksTypesList", linksTypesList);
        mav.addObject("headlinkList", headlinkList);
        mav.addObject("footerlinkList", footerlinkList);
        List<LinksTypeArrayList> linksTypeArrayListList = new ArrayList<>();
        for (LinksType value : linksTypesList) {
            //System.out.println(value);
            LinksTypeArrayList linksTypeArrayList = new LinksTypeArrayList();
            List<Links> linksList = linksService.getLinksAllByTypeID(value.getType_id());
            linksTypeArrayList.setLinksArrayList(linksList);
            linksTypeArrayList.setLinksType(value);
            linksTypeArrayListList.add(linksTypeArrayList);
        }
        /*for (LinksTypeArrayList value : linksTypeArrayListList) {
            //System.out.println(value);
        }*/

        mav.addObject("bodyLinksList", linksTypeArrayListList);

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        mav.addObject("dateYear", year);

        //设置视图，实现页面跳转
        mav.setViewName("index");
        return mav;
    }
}
